package org.sgx.gapigui.client.ui.actions.drive;

import org.sgx.gapigui.client.ui.editableModels.drive.EncryptedFileForm;
import org.sgx.gapigui.client.ui.extra.sjcl.StanfordCryptoLib;
import org.sgx.gwteditors.client.editor.props.PropertyHaver;
import org.sgx.gwteditors.client.impl1.complex.PropertyHaverEditor2;
import org.sgx.gwteditors.client.impl1.misc.richtexttoolbar.RichTextToolbar;
import org.sgx.gwteditors.client.util.AcceptCancelDialogListener;
import org.sgx.gwteditors.client.util.GUIUtil;

import com.google.gwt.core.client.Callback;
import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.ScriptInjector;
import com.google.gwt.core.client.ScriptInjector.FromUrl;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.RichTextArea;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class DriveFileEncrypt extends Composite {

	private static DriveFileEncryptUiBinder uiBinder = GWT.create(DriveFileEncryptUiBinder.class);

	interface DriveFileEncryptUiBinder extends UiBinder<Widget, DriveFileEncrypt> {
	}

	@UiField
	Button saveButton;
	@UiField
	VerticalPanel mainPanel;
	private RichTextArea area;
	private RichTextToolbar toolbar;

	public DriveFileEncrypt() {
		initWidget(uiBinder.createAndBindUi(this));
		area = new RichTextArea();
		area.ensureDebugId("cwRichText-area");
		area.setSize("100%", "14em");
		toolbar = new RichTextToolbar(area);
		toolbar.ensureDebugId("cwRichText-toolbar");
		toolbar.setWidth("100%");

		mainPanel.add(toolbar);
		mainPanel.add(area);
		
		saveButton.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				doSave();
			}
		}); 
		saveButton.setEnabled(false); 
		loadCryptoLib();

	}

	private void loadCryptoLib() {
StanfordCryptoLib.load(new Callback<Void, Exception>() {			
	@Override
	public void onSuccess(Void result) {
		saveButton.setEnabled(true); 
//		Window.alert("ready");
		//ready to work
		String password = "banana4", plainText="En algún lugar de la mancha"; 
		String encrypted = StanfordCryptoLib.encrypt(password, plainText); 
		String decrypted = StanfordCryptoLib.decrypt(password, encrypted); 
//		System.out.println(encrypted+" - "+decrypted);
//		Window.alert("ready2 : "+encrypted+" - "+decrypted);
	}			
	@Override
	public void onFailure(Exception reason) {
		Window.alert("Couldn't load the Crypto Library from http://crypto.stanford.edu/sjcl/"); 
	}
}); 
		
//		FromUrl injector = ScriptInjector.fromUrl("http://crypto.stanford.edu/sjcl/sjcl.js");		
//		injector.setCallback(new Callback<Void, Exception>() {			
//			@Override
//			public void onSuccess(Void result) {
//				saveButton.setEnabled(true); 
//			}			
//			@Override
//			public void onFailure(Exception reason) {
//				Window.alert("Couldn't load the Crypto Library from http://crypto.stanford.edu/sjcl/"); 
//			}
//		}).setWindow(ScriptInjector.TOP_WINDOW).inject(); 
	}

	protected void doSave() {
		PropertyHaverEditor2<PropertyHaver> ed = new PropertyHaverEditor2<PropertyHaver>();
		EncryptedFileForm bean = GWT.create(EncryptedFileForm.class);  
		ed.load(bean); 
		DialogBox db = GUIUtil.acceptCancelDialog("Save encrypted file", ed.getWidget(), new AcceptCancelDialogListener() {
			
			@Override
			public void notifyCancel(DialogBox db) {
				db.hide(); 
			}
			
			@Override
			public void notifyAccept(DialogBox db) {
				db.hide(); 
				doSaveFile();
			}
		}); 
	}

	protected void doSaveFile() {
		// TODO Auto-generated method stub
		
	}

}
